using JT808Protocol.Abstracts;
using JT808Protocol.Handlers;
using JT808Protocol.Methods;
using JT808Protocol.Models.JsonConverters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace UnitTest
{
    [TestClass]
    public class TestProgram
    {
        [TestMethod]
        public void TestMethod1()
        {
            JT808Parser jtSerializer = new JT808Parser();
            string str = string.Empty;
            //str = "7E,07,04,41,13,01,00,00,00,00,04,01,59,09,64,87,00,11,00,04,01,00,42,00,00,00,00,00,04,10,03,01,58,4D,68,06,CF,38,68,00,29,00,00,01,47,21,08,24,12,01,10,01,04,00,00,1E,DC,02,02,00,00,03,02,01,90,25,04,00,00,00,00,30,01,4B,31,01,1F,14,04,00,00,00,01,15,04,00,00,00,3D,00,42,00,00,00,00,00,04,10,03,01,58,4D,68,06,CF,38,68,00,29,00,00,01,47,21,08,24,12,01,25,01,04,00,00,1E,DE,02,02,00,00,03,02,01,90,25,04,00,00,00,00,30,01,4B,31,01,1F,14,04,00,00,00,01,15,04,00,00,00,3D,00,42,00,00,00,00,00,04,10,03,01,58,4D,68,06,CF,38,68,00,29,00,00,01,47,21,08,24,12,01,40,01,04,00,00,1E,DF,02,02,00,00,03,02,01,90,25,04,00,00,00,00,30,01,4B,31,01,1F,14,04,00,00,00,01,15,04,00,00,00,3D,00,42,00,00,00,00,00,04,10,03,01,58,4D,68,06,CF,38,68,00,28,00,00,01,47,21,08,24,12,01,55,01,04,00,00,1E,E1,02,02,00,00,03,02,01,90,25,04,00,00,00,00,30,01,4B,31,01,1F,14,04,00,00,00,01,15,04,00,00,00,3D,EF,7E";
            //str = "7E,02,00,40,42,01,00,00,00,00,04,01,59,09,64,87,00,18,00,00,00,00,00,04,10,03,01,58,4C,DA,06,CF,38,31,00,2E,00,00,00,00,21,10,12,14,53,42,01,04,00,00,25,3B,02,02,00,00,03,02,00,00,25,04,00,00,00,00,30,01,47,31,01,19,14,04,00,00,00,01,15,04,00,00,00,3D,E6,7E";
            str = "7E,01,04,01,B1,04,00,46,61,64,81,00,34,00,02,31,00,00,00,01,04,00,00,00,B4,00,00,00,02,04,00,00,00,05,00,00,00,03,04,00,00,00,03,00,00,00,04,04,00,00,00,00,00,00,00,05,04,00,00,00,00,00,00,00,06,04,00,00,00,00,00,00,00,07,04,00,00,00,00,00,00,00,10,05,43,4D,4E,45,54,00,00,00,11,00,00,00,00,12,00,00,00,00,13,0E,6A,74,31,2E,67,67,68,79,70,74,2E,6E,65,74,00,00,00,14,00,00,00,00,15,00,00,00,00,16,00,00,00,00,17,0E,6A,74,32,2E,67,67,68,79,70,74,2E,6E,65,74,00,00,00,18,04,00,00,1B,60,00,00,00,19,04,00,00,1B,60,00,00,00,20,04,00,00,00,00,00,00,00,21,04,00,00,00,00,00,00,00,22,04,00,00,00,00,00,00,00,28,04,00,00,00,00,00,00,00,27,04,00,00,00,78,00,00,00,29,04,00,00,00,12,00,00,00,2C,04,00,00,00,00,00,00,00,50,04,00,00,00,00,00,00,00,52,04,00,00,00,00,00,00,00,53,04,00,00,00,00,00,00,00,55,04,00,00,00,78,00,00,00,56,04,00,00,00,0A,00,00,00,57,04,00,00,38,40,00,00,00,58,04,00,00,00,00,00,00,00,59,04,00,00,04,B0,00,00,00,5A,04,00,00,00,00,00,00,00,5B,02,00,32,00,00,00,5C,02,07,08,00,00,00,70,04,00,00,00,00,00,00,00,71,04,00,00,00,00,00,00,00,72,04,00,00,00,00,00,00,00,73,04,00,00,00,00,00,00,00,74,04,00,00,00,00,00,00,00,80,04,00,00,3D,96,00,00,00,81,02,00,2C,00,00,00,82,02,01,2C,00,00,00,83,0C,D4,C1,42,4C,47,30,31,36,00,00,00,00,00,00,00,84,01,01,00,00,00,90,01,03,00,00,10,18,01,01,00,00,10,22,04,08,01,00,05,00,00,10,23,01,00,A2,7E";
            var package = jtSerializer.Parse(str);

            //var customConvertor = new JsonStringEnumConverter();
            //customConvertor.CanConvert(typeof(byte));

            string strJson = JsonSerializer.Serialize(package, new JsonSerializerOptions
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                WriteIndented = true,
                IgnoreNullValues = true,
                Converters = {
                }
            });
            var serializeOptions = new JsonSerializerOptions();
            serializeOptions.Converters.Add(new JT808DefaultConverter());
            //string strJson = JsonSerializer.Serialize(package, serializeOptions);
            //package = JsonSerializer.Deserialize<JT808Package>(strJson, serializeOptions);

            Assert.IsNotNull(package);
        }

        [TestMethod]
        public void TestProvider()
        {
            var handlers = JT808HandlerProvider.GetHandlers();
            var succ = false;
            foreach (var h in handlers)
            {
                MsgHandlerBase abstractMsgHandler = (MsgHandlerBase)h;
                succ = true;
            }
            Assert.IsTrue(succ); ;
        }
    }
}